Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Listing 12.5 PAINTPIC.FRM.
    Option Explicit

    ‘ This constant should be predefined by VB, like vbBlackness
    ‘ and vbSrcCopy, but it is not, so we must do it ourselves.
    Const vbWhiteness = &HFF0062

    ‘ Use smaller values for greater magnification.
    Const WIDE = 0.3
    Const HIGH = 0.3

    Private Sub Form_Load()

    ‘ Make the form the same height and a bit more
    ‘ than twice the width of the Picture Box.
    Form1.Width = 2.1 * Picture1.Width
    Form1.Height = 1.1 * Picture1.Height

    ‘ Put the first Picture Box on the left side of the form.
    Picture1.Left = 0
    Picture1.Top = 0

    ‘ Make the second Picture Box the same size
    ‘ as the first one (which is the size of the loaded picture
    ‘ because its AutoSize property is True).
    Picture2.Width = Picture1.Width
    Picture2.Height = Picture1.Height

    ‘ Put the second Picture Box on the right side of the form.
    Picture2.Top = 0
    Picture2.Left = Form1.ScaleWidth - Picture2.Width

    End Sub

    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, _
        X As Single, Y As Single)

    Dim XSrc As Single, YSrc As Single
    Dim X1 As Single, Y1 As Single, W1 As Single, H1 As Single
    Dim row As Integer, col As Integer

    If Button = vbLeftButton Then      ‘ Left button

        ‘ Calculate the source rectangle X coordinate. Be sure the
        ‘ resulting rectangle does not extend past the Picture Box
        ‘ edge on either the left or the right.
        XSrc = X - ((Picture1.Width * WIDE) / 2)
        If XSrc < 0 Then XSrc = 0
        If (XSrc + (WIDE * Picture1.Width)) > Picture1.Width Then
            XSrc = Picture1.Width * (1 - WIDE)
        End If

        ‘ Do the same for the Y coordinate.
        YSrc = Y - ((Picture1.Height * HIGH) / 2)
        If YSrc < 0 Then YSrc = 0
        If (YSrc + (HIGH * Picture1.Height) > Picture1.Height) Then
            YSrc = Picture1.Height * (1 - HIGH)
        End If

        ‘ Paint the contents of the rectangle to the destination Picture Box.
        Picture2.PaintPicture Picture1.picture, 0, 0, Picture2.WIDTH, _
           Picture2.HEIGHT, XSrc, YSrc, Picture1.Width * WIDE, _
           Picture1.Height * HIGH, vbSrcCopy
    End If

    If Button = vbRightButton Then      ‘ Right button.
        W1 = Picture2.Width / 3
        H1 = Picture2.Height / 3
        ‘ The black background.
        Picture2.PaintPicture Picture1.Picture, 0, 0, Picture2.Width, _
    Picture2.Height, 0, 0, , , vbBlackness
        For row = 1 To 3
            For col = 1 To 3
            X1 = (row - 0.95) * W1
            Y1 = (col - 0.95) * H1
                ‘ The white border.
                Picture2.PaintPicture Picture1.Picture, (row - 0.95) * W1, _
                    (col - 0.95) * H1, W1 * 0.9, H1 * 0.9, , , , , vbWhiteness
                ‘ The picture.
                Picture2.PaintPicture Picture1.Picture, (row - 0.9) * W1, _
                    (col - 0.9) * H1, W1 * 0.8, H1 * 0.8, 0, 0, _
                    Picture1.Width, Picture1.Height, vbSrcCopy
            Next col
        Next row
    End If

    End Sub
### Using The Image Control The Image control is another way to display images on your forms. When displaying bitmaps and metafiles, the Image control behaves very much like a Picture Box control. It has only some of the Picture Box’s capabilities, which means that an Image control is faster and takes up less of your precious system resources. Image controls are your best bet for displaying pictures; use a Picture Box only when you need its extra features. Table 12.5 will give you an idea of the differences between the two controls. The Image control is specialized for displaying pictures, and that’s about it. Note, however, that the Image control has one capability that the Picture Box lacks: the ability to stretch or shrink an image to fit the Image control’s dimensions, without resorting to the PaintPicture method. This is controlled by the Image control’s Stretch property, which can be set to True or False. ### The Picture Object The best way to think of the Picture object is as a temporary storage location for images. That’s about all it can do; it does not display or manipulate images in any way. You can load a Picture object with the same image formats that are supported by the Picture Box control. Then, as needed, you can transfer the images to a Picture Box control for display. Because a Picture object uses much less memory and resources, this approach is preferred to the technique of using invisible Picture Box controls as a staging location for images. To demonstrate, I’ll show you how to use the Picture object to create a simple animation. To start, I use a graphics program to create a series of simple images; the first one is shown in Figure 12.9. The other three images are essentially identical, but have the black bar pointing at different angles. I saved these images as SPIN1.BMP through SPIN4.BMP. Table 12.5 The Picture Box control vs. the Image control.
Feature | Picture Box | Image
--- | --- | ---
Load pictures at design- or runtime by setting Picture property | Yes | Yes
Load pictures with LoadPicture statement | Yes | Yes
Use as source/destination of PaintPicture method | Yes | No
Display graphics output created with Line, Circle, and Pset methods | Yes | No
Display text created with the Print method | Yes | No
Use to group other controls | Yes | No
Can optionally adjust control size to fit loaded image | Yes | No
Can optionally adjust image size to fit control size | No | Yes
AutoRedraw available | Yes | No
The project’s form contains one Picture Box, one Timer, and one control array of three Command Buttons. The objects and properties are summarized in Listing 12.6. I called both the project and the form PICTUREDEMO. Listing 12.6 Objects and properties in PICTUREDEMO.FRM.
    Begin VB.Form Form1
       Caption         =   “Picture Object Demo”
       Begin VB.CommandButton Command1
          Caption         =   “Exit”
          Index           =   2
       End
       Begin VB.CommandButton Command1
          Caption         =   “Stop”
          Index           =   1
       End
       Begin VB.CommandButton Command1
          Caption         =   “Start”
          Index           =   0

       End
       Begin VB.Timer Timer1
       End
       Begin VB.PictureBox Picture1
       End
    End
Figure 12.9 The first animation image.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.